package com.edu.juc.cas.atomic;

import com.edu.juc.bean.ClickNumber;

import java.util.concurrent.CountDownLatch;

public class ClickNumberDemo {
    public static final int _100W = 1000000;
    public static final int THREAD_COUNT = 50;

    public static void main(String[] args) {

        CountDownLatch countDownLatch1 = new CountDownLatch(THREAD_COUNT);
        CountDownLatch countDownLatch2 = new CountDownLatch(THREAD_COUNT);
        CountDownLatch countDownLatch3 = new CountDownLatch(THREAD_COUNT);
        CountDownLatch countDownLatch4 = new CountDownLatch(THREAD_COUNT);
        extracted(countDownLatch1, 1);
        extracted(countDownLatch2, 2);
        extracted(countDownLatch3, 3);
        extracted(countDownLatch4, 4);
    }

    private static void extracted(CountDownLatch countDownLatch, int flag) {
        ClickNumber clickNumber = new ClickNumber();
        new Thread(() -> {
            long start = System.currentTimeMillis();
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            switch (flag) {
                case 1 -> System.out.println("synchronized 耗时: " + (System.currentTimeMillis() - start));
                case 2 -> System.out.println("AtomicLong 耗时: " + (System.currentTimeMillis() - start));
                case 3 -> System.out.println("LongAccumulator 耗时: " + (System.currentTimeMillis() - start));
                case 4 -> System.out.println("LongAdder 耗时: " + (System.currentTimeMillis() - start));
            }
        }).start();
        if (flag == 1) {
            for (int i = 0; i < THREAD_COUNT; i++) {
                new Thread(() -> {
                    for (int j = 0; j < _100W; j++) {
                        clickNumber.increaseSynchronized();
                    }
                    countDownLatch.countDown();
                }).start();
            }
        } else if (flag == 2) {
            for (int i = 0; i < THREAD_COUNT; i++) {
                new Thread(() -> {
                    for (int j = 0; j < _100W; j++) {
                        clickNumber.increaseAtomicLong();
                    }
                    countDownLatch.countDown();
                }).start();
            }
        } else if (flag == 3) {
            for (int i = 0; i < THREAD_COUNT; i++) {
                new Thread(() -> {
                    for (int j = 0; j < _100W; j++) {
                        clickNumber.increaseLongAccumulator();
                    }
                    countDownLatch.countDown();
                }).start();
            }
        } else if (flag == 4) {
            for (int i = 0; i < THREAD_COUNT; i++) {
                new Thread(() -> {
                    for (int j = 0; j < _100W; j++) {
                        clickNumber.increaseLongAdder();
                    }
                    countDownLatch.countDown();
                }).start();
            }
        }
    }
}
